package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.PigHouseDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPighouseDAO;

import cn.edu.scau.cmi.hombio.domain.PigHouse;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskPighouse;

import cn.edu.scau.cmi.hombio.service.TaskPighouseService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for TaskPighouse entities
 * 
 */

@Controller("TaskPighouseRestController")

public class TaskPighouseRestController {

	/**
	 * DAO injected by Spring that manages PigHouse entities
	 * 
	 */
	@Autowired
	private PigHouseDAO pigHouseDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskPighouse entities
	 * 
	 */
	@Autowired
	private TaskPighouseDAO taskPighouseDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for TaskPighouse entities
	 * 
	 */
	@Autowired
	private TaskPighouseService taskPighouseService;

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Get Task entity by TaskPighouse
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}/task", method = RequestMethod.GET)
	@ResponseBody
	public Task getTaskPighouseTask(@PathVariable Integer taskpighouse_id) {
		return taskPighouseDAO.findTaskPighouseByPrimaryKey(taskpighouse_id).getTask();
	}

	/**
	* Create a new Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}/task", method = RequestMethod.POST)
	@ResponseBody
	public Task newTaskPighouseTask(@PathVariable Integer taskpighouse_id, @RequestBody Task task) {
		taskPighouseService.saveTaskPighouseTask(taskpighouse_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Delete an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}/task/{task_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPighouseTask(@PathVariable Integer taskpighouse_id, @PathVariable Integer related_task_id) {
		taskPighouseService.deleteTaskPighouseTask(taskpighouse_id, related_task_id);
	}

	/**
	* Show all TaskPighouse entities
	* 
	*/
	@RequestMapping(value = "/TaskPighouse", method = RequestMethod.GET)
	@ResponseBody
	public List<TaskPighouse> listTaskPighouses() {
		return new java.util.ArrayList<TaskPighouse>(taskPighouseService.loadTaskPighouses());
	}

	/**
	* Delete an existing TaskPighouse entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPighouse(@PathVariable Integer taskpighouse_id) {
		TaskPighouse taskpighouse = taskPighouseDAO.findTaskPighouseByPrimaryKey(taskpighouse_id);
		taskPighouseService.deleteTaskPighouse(taskpighouse);
	}

	/**
	* Create a new TaskPighouse entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse", method = RequestMethod.POST)
	@ResponseBody
	public TaskPighouse newTaskPighouse(@RequestBody TaskPighouse taskpighouse) {
		taskPighouseService.saveTaskPighouse(taskpighouse);
		return taskPighouseDAO.findTaskPighouseByPrimaryKey(taskpighouse.getId());
	}

	/**
	* Select an existing TaskPighouse entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}", method = RequestMethod.GET)
	@ResponseBody
	public TaskPighouse loadTaskPighouse(@PathVariable Integer taskpighouse_id) {
		return taskPighouseDAO.findTaskPighouseByPrimaryKey(taskpighouse_id);
	}

	/**
	* Create a new PigHouse entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}/pigHouse", method = RequestMethod.POST)
	@ResponseBody
	public PigHouse newTaskPighousePigHouse(@PathVariable Integer taskpighouse_id, @RequestBody PigHouse pighouse) {
		taskPighouseService.saveTaskPighousePigHouse(taskpighouse_id, pighouse);
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouse.getId());
	}

	/**
	* Get PigHouse entity by TaskPighouse
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}/pigHouse", method = RequestMethod.GET)
	@ResponseBody
	public PigHouse getTaskPighousePigHouse(@PathVariable Integer taskpighouse_id) {
		return taskPighouseDAO.findTaskPighouseByPrimaryKey(taskpighouse_id).getPigHouse();
	}

	/**
	* Save an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}/pigHouse", method = RequestMethod.PUT)
	@ResponseBody
	public PigHouse saveTaskPighousePigHouse(@PathVariable Integer taskpighouse_id, @RequestBody PigHouse pighouse) {
		taskPighouseService.saveTaskPighousePigHouse(taskpighouse_id, pighouse);
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouse.getId());
	}

	/**
	* Save an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}/task", method = RequestMethod.PUT)
	@ResponseBody
	public Task saveTaskPighouseTask(@PathVariable Integer taskpighouse_id, @RequestBody Task task) {
		taskPighouseService.saveTaskPighouseTask(taskpighouse_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* View an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}/pigHouse/{pighouse_id}", method = RequestMethod.GET)
	@ResponseBody
	public PigHouse loadTaskPighousePigHouse(@PathVariable Integer taskpighouse_id, @PathVariable Integer related_pighouse_id) {
		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouse_id, -1, -1);

		return pighouse;
	}

	/**
	* Delete an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}/pigHouse/{pighouse_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPighousePigHouse(@PathVariable Integer taskpighouse_id, @PathVariable Integer related_pighouse_id) {
		taskPighouseService.deleteTaskPighousePigHouse(taskpighouse_id, related_pighouse_id);
	}

	/**
	* Save an existing TaskPighouse entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse", method = RequestMethod.PUT)
	@ResponseBody
	public TaskPighouse saveTaskPighouse(@RequestBody TaskPighouse taskpighouse) {
		taskPighouseService.saveTaskPighouse(taskpighouse);
		return taskPighouseDAO.findTaskPighouseByPrimaryKey(taskpighouse.getId());
	}

	/**
	* View an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPighouse/{taskpighouse_id}/task/{task_id}", method = RequestMethod.GET)
	@ResponseBody
	public Task loadTaskPighouseTask(@PathVariable Integer taskpighouse_id, @PathVariable Integer related_task_id) {
		Task task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		return task;
	}
}